var ctx=window.daijia.ctx;
$(function () {
	
	$("#typeSelect").change(function(){
		
		window.location=$(this).val();
		
	});

	$("#submit_button").click(function(){
		
		if($("#name").val()==""){
			alert("请填写名称。");
			$("#name").focus();
			return false;
		}
		
		var formable=true;
		var numPoint=/^(\d+)(.[0-9]{0,1})?$/;
		
		$("#fieldset-div fieldset").each(function(index,element){
			$(element).find(".moneyConfig_div").each(function(i,e){
				var fromMoney=$(e).find(".fromMoney");
				var toMoney=$(e).find(".toMoney");
				var type=$(e).find(".driverLevelMoneyType");
				var moneyUnit=$(e).find(".moneyUnit");
				var money=$(e).find(".money");
				var commission=$(e).find(".commission");
				
				if(fromMoney.val()=="" || !numPoint.test(fromMoney.val())){
	    			alert("请填写正确的金额。");
	    			fromMoney.focus();
	    			formable=false;
	    			return false;
	    		}
				if(toMoney.val()=="" || !numPoint.test(toMoney.val())){
					alert("请填写正确的金额。");
					toMoney.focus();
	    			formable=false;
	    			return false;
				}
				if(parseFloat(toMoney.val())<=parseFloat(fromMoney.val())){
					alert("结束金额必须大于起始金额。");
					toMoney.focus();
	    			formable=false;
	    			return false;
				}
				if(type.val()=="guding" && (moneyUnit.val()=="" || !numPoint.test(moneyUnit.val()))){
					alert("请填写正确的阀值金额。");
					moneyUnit.focus();
	    			formable=false;
	    			return false;
				}
				if(type.val()=="guding" && moneyUnit.val()==0){
					alert("该阀值金额必须大于0。");
					moneyUnit.focus();
	    			formable=false;
	    			return false;
				}
				if(parseFloat(moneyUnit.val())>(parseFloat(toMoney.val())-parseFloat(fromMoney.val()))){
					alert("该阀值金额必须小于等于结束金额减去起始金额。");
					moneyUnit.focus();
	    			formable=false;
	    			return false;
				}
				if(type.val()=="guding" && (money.val()=="" || !numPoint.test(money.val()))){
					alert("请填写正确的提成。");
					money.focus();
	    			formable=false;
	    			return false;
				}
				if(type.val()=="ticheng" && (commission.val()=="" || !numPoint.test(commission.val()))){
					alert("请填写正确的提成。");
					commission.focus();
	    			formable=false;
	    			return false;
				}
				
				//上一级
				if(i>0){
					if($($(element).find(".moneyConfig_div")[i-1]).length>0){
						var last_toMoney=$($(element).find(".moneyConfig_div")[i-1]).find(".toMoney");
						if(parseFloat(fromMoney.val())!=parseFloat(last_toMoney.val())){
							alert("该金额必须等于上一级结束金额。");
							fromMoney.focus();
			    			formable=false;
			    			return false;
						}
					}
				}
				
			});
			
			if(!formable){
        		return false;
        	}
			
		});
		
		if(!formable){
    		return false;
    	}
		
		$("#fieldset-div fieldset").each(function(index,element){
			$(element).find(".moneyConfig_div").each(function(i,e){
				$(e).find(".fromMoney").attr("name",$(element).attr("data-name")+".driverLevelMoneyDetails["+i+"].fromMoney");
				$(e).find(".toMoney").attr("name",$(element).attr("data-name")+".driverLevelMoneyDetails["+i+"].toMoney");
				$(e).find(".driverLevelMoneyType").attr("name",$(element).attr("data-name")+".driverLevelMoneyDetails["+i+"].driverLevelMoneyType");
				$(e).find(".moneyUnit").attr("name",$(element).attr("data-name")+".driverLevelMoneyDetails["+i+"].moneyUnit");
				$(e).find(".money").attr("name",$(element).attr("data-name")+".driverLevelMoneyDetails["+i+"].money");
				$(e).find(".commission").attr("name",$(element).attr("data-name")+".driverLevelMoneyDetails["+i+"].commission");
    		});
    	});

		$(this).attr("disabled",true);
	        
        $.post($("#submit_form").attr("action"),$("#submit_form").serialize(),function(result){
			if(result.success){
				window.location=ctx+"/driverlevel/list";
			}
			else{
				$.scojs_message(result.message, $.scojs_message.TYPE_ERROR);
			}
		});

    	return false;
	});
	
	//改变提成类型
	$(document).on('change','.driverLevelMoneyType',function(){
    	if($(this).val()=="guding"){
    		$(this).parent().find(".commission").hide();
    		$(this).parent().find(".commission_span").hide();
    		$(this).parent().find(".money").show();
    		$(this).parent().find(".money_span").show();
    		$(this).parent().find(".moneyUnit").attr("disabled",false);
    	}
    	else if($(this).val()=="ticheng"){
    		$(this).parent().find(".money").hide();
    		$(this).parent().find(".money_span").hide();
    		$(this).parent().find(".commission").show();
    		$(this).parent().find(".commission_span").show();
    		$(this).parent().find(".moneyUnit").val("").attr("disabled",true);
    	}
    });
	
	//增加金额区间
	$(document).on('click','.add-money-area',function(){
		
		var numPoint=/^(\d+)(.[0-9]{0,1})?$/;
		var toMoney=$($(this).parent().parent().parent().find(".moneyConfig_div:last")).find(".toMoney");
		if(toMoney.val()=="" || !numPoint.test(toMoney.val())){
			alert("请填写正确的金额。");
			toMoney.focus();
			return false;
		}
		
		
		var content='<div class="form-group">'+
						'<label class="col-sm-3 control-label">'+
							'<button type="button" class="btn btn-default btn-xs del-money-area"><i class="glyphicon glyphicon-minus"></i> 删除</button>'+
						'</label>'+
						'<div class="col-sm-8 moneyConfig_div">'+
					        '<div class="input-group">'+
					        	'<span class="input-group-addon">从</span>'+
					        	'<input class="form-control fromMoney" type="number" min="0" value="'+toMoney.val()+'"/>'+
					        	'<span class="input-group-addon">元到</span>'+
					        	'<input class="form-control toMoney" type="number" min="0"/>'+
					        	'<span class="input-group-addon">元，提成方式为</span>'+
					        	'<select class="form-control driverLevelMoneyType" style="width:120px;">'+
					        		'<option value="guding">固定提成</option>'+
					        		'<option value="ticheng">比例提成</option>'+
					        	'</select>'+
					            '<span class="input-group-addon moneyUnit_span">每</span>'+
					            '<input class="form-control moneyUnit" type="number" min="0"/>'+
					        	'<span class="input-group-addon">元提</span>'+
					        	'<input class="form-control money" type="number" min="0"/>'+
					        	'<span class="input-group-addon money_span">元</span>'+
					        	'<input class="form-control commission" type="number" min="0" style="display:none;"/>'+
					        	'<span class="input-group-addon commission_span" style="display:none;">%</span>'+
					        '</div>'+
					    '</div>'+
					'</div>';
    	$(this).parent().parent().parent().append(content);
    });
	
	$(document).on('click','.del-money-area',function(){
		$(this).parent().parent().remove();
	});
	
	

    $(document).on('click','.time-delete',function(){
    	$(this).parent().parent().remove();
    });
    
    var time_index=$("#fieldset-div fieldset").length;
    $(".time-add").click(function(){
    		
    	var lastStartHour=$($("#fieldset-div fieldset:last select")[0]).val();
    	var lastStartMinute=$($("#fieldset-div fieldset:last select")[1]).val();
    	var lastEndHour=$($("#fieldset-div fieldset:last select")[2]).val();
    	var lastEndMinute=$($("#fieldset-div fieldset:last select")[3]).val();
    	
    	var newStartHour,newStartMinute,newEndHour,newEndMinute;
    	
    	if(lastEndMinute==59){
    		newEndMinute=0;
    		newEndHour=parseInt(lastEndHour)+1;
    	}
    	else{
    		newEndHour=lastEndHour;
    		newEndMinute=parseInt(lastEndMinute)+1;
    	}
    	if(newEndHour==24){
    		newEndHour=0;
    	}
    	
    	if(lastStartMinute==0){
    		newStartMinute=59;
    		newStartHour=parseInt(lastStartHour)-1;
    	}
    	else{
    		newStartHour=lastStartHour;
    		newStartMinute=parseInt(lastStartMinute)-1;
    	}
    	
    	if(newStartHour==-1){
    		newStartHour=23;
    	}
    	
    	var content='<div class="form-group">'+
					'<label class="col-sm-3 control-label">'+
						'<button type="button" class="btn btn-default btn-xs add-money-area"><i class="glyphicon glyphicon-plus"></i> 添加</button>'+
					'</label>'+
					'<div class="col-sm-8 moneyConfig_div">'+
				        '<div class="input-group">'+
				        	'<span class="input-group-addon">从</span>'+
				        	'<input class="form-control fromMoney" type="number" min="0"/>'+
				        	'<span class="input-group-addon">元到</span>'+
				        	'<input class="form-control toMoney" type="number" min="0"/>'+
				        	'<span class="input-group-addon">元，提成方式为</span>'+
				        	'<select class="form-control driverLevelMoneyType" style="width:120px;">'+
				        		'<option value="guding">固定提成</option>'+
				        		'<option value="ticheng">比例提成</option>'+
				        	'</select>'+
				            '<span class="input-group-addon moneyUnit_span">每</span>'+
				            '<input class="form-control moneyUnit" type="number" min="0"/>'+
				        	'<span class="input-group-addon">元提</span>'+
				        	'<input class="form-control money" type="number" min="0"/>'+
				        	'<span class="input-group-addon money_span">元</span>'+
				        	'<input class="form-control commission" type="number" min="0" style="display:none;"/>'+
				        	'<span class="input-group-addon commission_span" style="display:none;">%</span>'+
				        '</div>'+
				    '</div>'+
				'</div>';
    		
		var newitem = '<fieldset data-name="driverLevelMoneyTimeDetails['+time_index+']">'+
						'<legend>分时段'+(time_index+1)+'</legend>'+
						'<small class="pull-right"><a href="javascript:;" class="time-delete">删除</a></small>'+
			            '<div class="form-group">'+
			            	'<label class="col-sm-3 control-label"><em>*</em>时间：</label>'+
			            	'<div class="col-sm-4">'+
					        	'<select name="driverLevelMoneyTimeDetails['+time_index+'].startHour">'+$("#item-hour-div").html()+'</select>'+
					        	'：'+
					        	'<select name="driverLevelMoneyTimeDetails['+time_index+'].startMinute">'+$("#item-minute-div").html()+'</select>'+
					        	'-'+
					        	'<select name="driverLevelMoneyTimeDetails['+time_index+'].endHour">'+$("#item-hour-div").html()+'</select>'+
					        	'：'+
					        	'<select name="driverLevelMoneyTimeDetails['+time_index+'].endMinute">'+$("#item-minute-div").html()+'</select>'+
					        '</div>'+
				         '</div>'+
				         content+
	                '</fieldset>';
    	
    	$("#fieldset-div").append(newitem);
    	
    	$($($("#fieldset-div fieldset:last select")[0]).find("option")[newEndHour]).attr("selected","selected");
    	$($($("#fieldset-div fieldset:last select")[1]).find("option")[newEndMinute]).attr("selected","selected");
    	$($($("#fieldset-div fieldset:last select")[2]).find("option")[newStartHour]).attr("selected","selected");
    	$($($("#fieldset-div fieldset:last select")[3]).find("option")[newStartMinute]).attr("selected","selected");
    	
    	time_index++;
    	
    });
    
});

